package com.puppet.building.service.impl;

import com.puppet.building.handler.MyAssert;
import com.puppet.building.mapper.BuildingMapper;
import com.puppet.building.mapper.HouseMapper;
import com.puppet.building.pojo.Building;
import com.puppet.building.pojo.enums.BuildingCode;
import com.puppet.building.service.BuildingService;
import com.puppet.building.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private HouseService houseService;

    @Override
    public List<Building>  getAllBuildings(String search) {
        return buildingMapper.getAllBuildings(search);
    }

    @Override
    public Integer addNewBuilding(Building building) {
        return buildingMapper.addNewBuilding(building);
    }

    @Override
    public Integer deleteBuilding(Integer buildingId) {
        Integer num = houseService.countHouseByBuildingId(buildingId);
        MyAssert.error(num > 0, BuildingCode.DELETE_BUILDING_FAIL.getMessage());
        return buildingMapper.deleteBuilding(buildingId);
    }

    @Override
    public Integer deleteBuildings(List<Integer> ids) {
        for (Integer id : ids) {
            Integer num = houseService.countHouseByBuildingId(id);
            MyAssert.error(num > 0, BuildingCode.DELETE_BUILDING_FAIL.getMessage());
        }
        return buildingMapper.deleteBuildings(ids);
    }

    @Override
    public Integer updateBuilding(Building building) {
       Integer result = buildingMapper.updateBuilding(building);
       MyAssert.error(result <= 0,BuildingCode.SAVE_FAIL.getMessage());
        return result;
    }

    @Override
    public Building getBuildingById(Integer buildingId) {
        return buildingMapper.getBuildingById(buildingId);
    }
}
